package CCRMClient.GUI;

import javax.swing.JPanel;

import java.awt.Color;

import javax.swing.border.LineBorder;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.SwingConstants;
import javax.swing.JTextField;

import java.awt.SystemColor;
import java.sql.ResultSet;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

import CCRMClient.ClientStart;
import CCRMClient.Controllers.CatalogManager;
import Common.SERVER_COMMANDS;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import net.proteanit.sql.DbUtils;

import org.eclipse.wb.swing.FocusTraversalOnArray;

import java.awt.Component;
import java.io.IOException;
import java.sql.ResultSet;

import javax.swing.ListSelectionModel;
/**
 * This class implements Search Product screen
 * @author Ori Ziv
 *
 */
public class ProductSearchScreen extends JPanel {
	private JLabel lblSearchProduct;
	private JLabel lblProductName;
	private JTextField textSearchField;
	private JButton btnSearch;
	private JButton btnGoBack;
	private JButton btnViewProduct;
	private JTable tblProducts;
	private JScrollPane scpProducts;
	private JLabel lblSearchResults;

	/**
	 * Create the panel.
	 */
	public ProductSearchScreen() {
		initialize();
	}
	
	public ProductSearchScreen(ResultSet searchResults, String search) {
		this();
		textSearchField.setText(search);
		loadRStoTable(searchResults);
		tblProducts.setEnabled(true);
		btnViewProduct.setEnabled(true);
	}
	
	private void loadRStoTable(ResultSet searchResults) {
		tblProducts.setModel(DbUtils.resultSetToTableModel(searchResults));
		JTableHeader th = tblProducts.getTableHeader();
		TableColumnModel tcm = th.getColumnModel();
		TableColumn tc = tcm.getColumn(0);
		tc.setHeaderValue("ID");
		
		th = tblProducts.getTableHeader();
		tcm = th.getColumnModel();
		tc = tcm.getColumn(1);
		tc.setHeaderValue("Name");
		
		th = tblProducts.getTableHeader();
		tcm = th.getColumnModel();
		tc = tcm.getColumn(2);
		tc.setHeaderValue("Status");
		
		th = tblProducts.getTableHeader();
		tcm = th.getColumnModel();
		tc = tcm.getColumn(3);
		tc.setHeaderValue("Base Price");
		
		th = tblProducts.getTableHeader();
		tcm = th.getColumnModel();
		tc = tcm.getColumn(4);
		tc.setHeaderValue("Description");
		
		tblProducts.getColumnModel().getColumn(0).setResizable(false);
		tblProducts.getColumnModel().getColumn(0).setPreferredWidth(37);
		tblProducts.getColumnModel().getColumn(0).setMinWidth(7);
		tblProducts.getColumnModel().getColumn(2).setResizable(false);
		tblProducts.getColumnModel().getColumn(2).setPreferredWidth(45);
		tblProducts.getColumnModel().getColumn(2).setMinWidth(10);
		tblProducts.getColumnModel().getColumn(4).setPreferredWidth(260);
		
		th.repaint();
	}
	private void initialize() {
		setSize(637, 577);
		setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		setBackground(Color.WHITE);
		setLayout(null);
		add(getLblSearchProduct());
		add(getLblProductName());
		add(getTextSearchField());
		add(getBtnSearch());
		add(getLblSearchResults());
		add(getScpProducts());
		add(getBtnGoBack());
		add(getBtnViewProduct());
	}
	JLabel getLblSearchProduct() {
		if (lblSearchProduct == null) {
			lblSearchProduct = new JLabel("Search Products");
			lblSearchProduct.setHorizontalAlignment(SwingConstants.CENTER);
			lblSearchProduct.setForeground(Color.RED);
			lblSearchProduct.setFont(new Font("David", Font.BOLD, 26));
			lblSearchProduct.setBounds(0, 11, 637, 27);
		}
		return lblSearchProduct;
	}
	JLabel getLblProductName() {
		if (lblProductName == null) {
			lblProductName = new JLabel("Product name or ID:");
			lblProductName.setFont(new Font("David", Font.BOLD, 20));
			lblProductName.setBounds(10, 45, 199, 29);
		}
		return lblProductName;
	}
	JTextField getTextSearchField() {
		if (textSearchField == null) {
			textSearchField = new JTextField();
			textSearchField.setText("fill in product name or ID");
			textSearchField.setColumns(10);
			textSearchField.setBackground(SystemColor.menu);
			textSearchField.setBounds(214, 49, 251, 20);
		}
		return textSearchField;
	}
	JButton getBtnSearch() {
		if (btnSearch == null) {
			btnSearch = new JButton("Search");
			btnSearch.addActionListener(new BtnSearchActionListener());
			btnSearch.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnSearch.setBounds(475, 48, 124, 23);
		}
		return btnSearch;
	}
	JButton getBtnGoBack() {
		if (btnGoBack == null) {
			btnGoBack = new JButton("Go Back");
			btnGoBack.addActionListener(new BtnGoBackActionListener());
			btnGoBack.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnGoBack.setBounds(10, 494, 124, 23);
		}
		return btnGoBack;
	}
	JButton getBtnViewProduct() {
		if (btnViewProduct == null) {
			btnViewProduct = new JButton("View Product");
			btnViewProduct.addActionListener(new BtnViewProductActionListener());
			btnViewProduct.setEnabled(false);
			btnViewProduct.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnViewProduct.setBounds(475, 495, 124, 23);
		}
		return btnViewProduct;
	}
	
	JTable getTblProducts() {
		if (tblProducts == null) {
			tblProducts = new JTable();
			tblProducts.setBackground(SystemColor.menu);
			tblProducts.setFont(new Font("David", Font.BOLD, 11));
			tblProducts.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			tblProducts.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
			tblProducts.setModel(new DefaultTableModel(
				new Object[][] {
					{null, null, null, null, null},
				},
				new String[] {
					"ID", "Name", "Status", "Base Price", "Description"
				}
			) {
				Class[] columnTypes = new Class[] {
					String.class, String.class, String.class, String.class, String.class
				};
				public Class getColumnClass(int columnIndex) {
					return columnTypes[columnIndex];
				}
				boolean[] columnEditables = new boolean[] {
					false, false, false, false, false
				};
				@Override
				public boolean isCellEditable(int row, int column) {
					return false;
				}
			});
			tblProducts.getColumnModel().getColumn(0).setResizable(false);
			tblProducts.getColumnModel().getColumn(0).setPreferredWidth(37);
			tblProducts.getColumnModel().getColumn(0).setMinWidth(7);
			tblProducts.getColumnModel().getColumn(2).setResizable(false);
			tblProducts.getColumnModel().getColumn(2).setPreferredWidth(45);
			tblProducts.getColumnModel().getColumn(2).setMinWidth(10);
			tblProducts.getColumnModel().getColumn(4).setPreferredWidth(260);
		}
		return tblProducts;
	}
	
	JScrollPane getScpProducts() {
		if (scpProducts == null) {
			scpProducts = new JScrollPane();
			scpProducts.setViewportBorder(null);
			scpProducts.setBounds(10, 131, 589, 327);
			scpProducts.setViewportView(getTblProducts());
		}
		return scpProducts;
	}
	
	JLabel getLblSearchResults() {
		if (lblSearchResults == null) {
			lblSearchResults = new JLabel("Search Results:");
			lblSearchResults.setHorizontalAlignment(SwingConstants.LEFT);
			lblSearchResults.setForeground(Color.BLACK);
			lblSearchResults.setFont(new Font("David", Font.BOLD, 22));
			lblSearchResults.setBounds(10, 85, 199, 27);
		}
		return lblSearchResults;
	}
	/**
	 * This inner class implements event listener for pressing Go Back button
	 * @author Ori Ziv
	 *
	 */
	private class BtnGoBackActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			ClientStart.panelSwicher(new EmployeePortal());
		}
	}
	/**
	 * This inner class implements event listener for pressing Search button
	 * @author Ori Ziv
	 *
	 */
	private class BtnSearchActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			try {
				String search = textSearchField.getText();
				if (search.isEmpty() || search.equals("fill in product name or ID")) throw new NullPointerException();
				try {
					ClientStart.client.handleMessageFromGUI(SERVER_COMMANDS.SEARCH_PRODUCT, CatalogManager.searchProduct(SERVER_COMMANDS.SEARCH_PRODUCT, search));
				} catch (IOException exp) {
					JOptionPane.showMessageDialog(null, "Lost connection with server", "Communication Error", JOptionPane.ERROR_MESSAGE);
					ClientStart.quitApp();
				}
			} catch (NullPointerException exp) {
				JOptionPane.showMessageDialog(null, "Fill in product name or ID", "Search Products", JOptionPane.INFORMATION_MESSAGE);
				textSearchField.setText("fill in product name or ID");
			}
		}
	}
	/**
	 * This inner class implements event listener for pressing View Product button
	 * @author Ori Ziv
	 *
	 */
	private class BtnViewProductActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			long rowIdx = tblProducts.getSelectedRow();
			if (rowIdx == -1)
				JOptionPane.showMessageDialog(null, "Please select a product", "View Product", JOptionPane.INFORMATION_MESSAGE);
			else {
				long productID = (long) tblProducts.getModel().getValueAt((int) rowIdx, 0);
				try {
					ClientStart.client.handleMessageFromGUI(SERVER_COMMANDS.GET_PRODUCT, CatalogManager.getProduct(productID));
				} catch (IOException e) {
					JOptionPane.showMessageDialog(null, "Lost connection with server", "Communication Error", JOptionPane.ERROR_MESSAGE);
					ClientStart.quitApp();
				}
			}
				
		}
	}
}
